<?php

function get_pcs_order_list(){
    $result = get_filter();
    if ($result === false)
    {
        /* 初始化分页参数 */
        $filter = array();
        $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'order_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
        $filter['order_sn']   = empty($_REQUEST['order_sn']) ? '' : trim($_REQUEST['order_sn']);
        $filter['status']     = (!is_numeric($_REQUEST['status']) || $_REQUEST['status']<0) ? -1 : intval($_REQUEST['status']);
        if (!empty($_GET['is_ajax']) && $_GET['is_ajax'] == 1)
        {
            $_REQUEST['supplier_name'] = json_str_iconv($_REQUEST['supplier_name']);
        }
        $filter['supplier_name']       = empty($_REQUEST['supplier_name']) ? '' : trim($_REQUEST['supplier_name']); 
        $filter['start_add_time']      = empty($_REQUEST['start_add_time']) ? '' : (strpos($_REQUEST['start_add_time'], '-') > 0 ?  local_strtotime($_REQUEST['start_add_time']) : $_REQUEST['start_add_time']);
        $filter['end_add_time']        = empty($_REQUEST['end_add_time']) ? '' : (strpos($_REQUEST['end_add_time'], '-') > 0 ?  local_strtotime($_REQUEST['end_add_time']) : $_REQUEST['end_add_time']);	
        $filter['start_deliver_time']  = empty($_REQUEST['start_deliver_time']) ? '' : (strpos($_REQUEST['start_deliver_time'], '-') > 0 ?  local_strtotime($_REQUEST['start_deliver_time']) : $_REQUEST['start_deliver_time']);
        $filter['end_deliver_time']    = empty($_REQUEST['end_deliver_time']) ? '' : (strpos($_REQUEST['end_deliver_time'], '-') > 0 ?  local_strtotime($_REQUEST['end_deliver_time']) : $_REQUEST['end_deliver_time']);

        $where = 'WHERE 1 ';
        if ($filter['order_sn'])
        {
            $where .= " AND po.order_sn LIKE '%" . mysql_like_quote($filter['order_sn']) . "%'";
        }
        if($filter['status']>=0){
        	$where .= " AND po.order_status=$filter[status]";
        }
        if ($filter['supplier_name'])
        {
            $where .= " AND s.supplier_name LIKE '%" . mysql_like_quote($filter['supplier_name']) . "%'";
        }  
        if ($filter['start_add_time'])
        {
            $where .= " AND po.add_time >= '$filter[start_add_time]'";
        }
        if ($filter['end_add_time'])
        {
            $where .= " AND po.add_time <= '$filter[end_add_time]'";
        }
        if ($filter['start_deliver_time'])
        {
            $where .= " AND po.deliver_time >= '$filter[start_deliver_time]'";
        }
        if ($filter['end_deliver_time'])
        {
            $where .= " AND po.deliver_time <= '$filter[end_deliver_time]'";
        }                     
        /* 查询记录总数，计算分页数 */
   		$sql = "SELECT COUNT(*) FROM " . $GLOBALS['os']->table('purchase_order') . " po LEFT JOIN " . 
   		       $GLOBALS['os']->table('supplier') . " s ON po.supplier_id=s.supplier_id " . $where;
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);
        $filter = page_and_size($filter);

        /* 查询记录 */
        $sql = "SELECT po.*,s.supplier_name FROM " . $GLOBALS['os']->table('purchase_order') . " po LEFT JOIN " . 
   		       $GLOBALS['os']->table('supplier') . " s ON po.supplier_id=s.supplier_id " . $where .
   		       " ORDER BY $filter[sort_by] $filter[sort_order]";
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
		$row['status']  				    = jsc_order_status('pcs', $row['order_status']);
		$row['formated_discount']  		    = sprintf('%s', floatval($row['discount']));
      	$row['formated_order_amount']	    = price_format($row['order_amount']);
      	$row['formated_add_time']           = local_date('Y-m-d', $row['add_time']);
      	$row['formated_deliver_time']       = local_date('Y-m-d', $row['deliver_time']);
      	$row['short_supplier_name']         = sub_str($row['supplier_name'], 10);
        $arr[] = $row;
    }
    return array('item' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);	
}

function pcs_order_info($order_id, $order_sn = '')
{
    /* 计算订单各种费用之和的语句 */
    $order_id = intval($order_id);
    if ($order_id > 0)
    {
        $sql = 'SELECT po.*,s.supplier_name FROM ' . $GLOBALS['os']->table('purchase_order') . 
               ' po LEFT JOIN ' . $GLOBALS['os']->table('supplier') . ' s ON po.supplier_id=s.supplier_id ' . 
               " WHERE po.order_id = '$order_id'";	
    }
    else
    {
        $sql = 'SELECT po.*,s.supplier_name FROM ' . $GLOBALS['os']->table('purchase_order') . 
        	   ' po LEFT JOIN ' . $GLOBALS['os']->table('supplier') . ' s ON po.supplier_id=s.supplier_id ' . 		
               " WHERE po.order_sn = '$order_sn'";	
    }
    $order = $GLOBALS['db']->getRow($sql);

    /* 格式化字段 */
    if ($order)
    {
    	$order['order_sn']   	          = substr($order['order_sn'], 0);
    	$order['supplier_name']   	  	  = substr($order['supplier_name'], 0);
        $order['depot_name']              = ' 默认 ';
        $order['seat_name']               = ' 默认 ';
        $order['discount']                = floatval($order['discount']);
        $order['formated_discount']       = sprintf('%s' , floatval($order['discount']));
     	$order['formated_add_time']       = local_date('Y-m-d', $order['add_time']); 
     	$order['formated_deliver_time']   = local_date('Y-m-d', $order['deliver_time']);       
        $order['formated_order_amount']   = price_format(abs($order['order_amount']), false);
		$order['status']                  = jsc_order_status('pcs', $order['order_status']);
    }
    return $order;
}

function pcs_order_goods($order_id){
	static $goods_list = array();
	$sql = "SELECT og.*,SUM(og.goods_number) subtotal_number,SUM(og.actual_mount) subtotal_mount,g.goods_name," . 
		   "g.goods_sn FROM " . $GLOBALS['os']->table('purchase_order_goods') . " og LEFT JOIN " . 
		   $GLOBALS['os']->table('goods') . " g ON og.goods_id=g.goods_id WHERE og.order_id=" . intval($order_id) . 
	       " GROUP BY og.goods_id ORDER BY rec_id DESC";
	$res = $GLOBALS['db']->query($sql);
	while ($row = $GLOBALS['db']->fetchRow($res)) {
		$goods_list[$row['goods_id']]['order_id']                =  $row['order_id'];
		$goods_list[$row['goods_id']]['goods_id']                =  $row['goods_id'];
		$goods_list[$row['goods_id']]['goods_name']              =  substr($row['goods_name'], 0);
		$goods_list[$row['goods_id']]['goods_sn']                =  substr($row['goods_sn'], 0);
		$goods_list[$row['goods_id']]['formated_norm_price']     =  price_format($row['norm_price']);		
		$goods_list[$row['goods_id']]['formated_discount']       =  sprintf('%s', floatval($row['discount']));
		$goods_list[$row['goods_id']]['formated_actual_price']   =  price_format($row['actual_price']);		  
		$goods_list[$row['goods_id']]['subtotal_number']         =  intval($row['subtotal_number']);
		$goods_list[$row['goods_id']]['formated_subtotal_mount'] =  price_format($row['subtotal_mount']);		      		
	}
	$sql = "SELECT og.*,g.goods_name,g.goods_sn,IFNULL(c.color_name,'默认') color_name,IFNULL(s.size_name,'默认') size_name,
	       IFNULL(b.brand_name, '') AS brand_name FROM " . $GLOBALS['os']->table('purchase_order_goods') . 
	       ' og LEFT JOIN ' . $GLOBALS['os']->table('goods') . ' g ON og.goods_id=g.goods_id LEFT JOIN ' . 
	       $GLOBALS['os']->table('color') . " c ON og.color_id=c.color_id LEFT JOIN " . $GLOBALS['os']->table('size') . 
	       ' s ON og.size_id=s.size_id LEFT JOIN ' . $GLOBALS['os']->table('brand') . ' b ON  g.brand_id=b.brand_id ' . 
	       ' WHERE og.order_id=' . intval($order_id) . ' ORDER BY og.rec_id DESC';
	$result = $GLOBALS['db']->query($sql);
	$arr_color = array();
	$arr_size = array();
	while ($row = $GLOBALS['db']->fetchRow($result)) {
		if(isset($goods_list[$row['goods_id']]) && is_array($goods_list[$row['goods_id']])){
			if(!is_array($arr_color[$row['goods_id']]) || !in_array($row['color_name'], $arr_color[$row['goods_id']])) 
					$arr_color[$row['goods_id']][]=$row['color_name'];
			if(!is_array($arr_size[$row['goods_id']]) || !in_array($row['size_name'], $arr_size[$row['goods_id']])) 
					$arr_size[$row['goods_id']][]=$row['size_name'];			
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['order_id']                =  $row['order_id'];
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['goods_id']                =  $row['goods_id'];
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['color_id']                =  $row['color_id'];
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['size_id']                 =  $row['size_id'];
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['goods_name']              =  substr($row['goods_name'], 0);
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['goods_sn']                =  substr($row['goods_sn'], 0);
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['formated_norm_price']     =  price_format($row['norm_price']);		
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['formated_discount']       =  sprintf('%s', floatval($row['discount']));
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['formated_actual_price']   =  price_format($row['actual_price']);		  
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['subtotal_number']         =  intval($row['goods_number']);
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['formated_subtotal_mount'] =  price_format(($row['actual_price'] * $row['goods_number']));	
		    $goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['goods_attr']              =  '颜色:' . $row['color_name'] . '&nbsp;&nbsp;尺码:' . $row['size_name'];
		}	
	    $goods_list[$row['goods_id']]['goods_attr']                                              =  '颜色:' . implode(',', $arr_color[$row['goods_id']]) . '&nbsp;&nbsp;尺码:' . implode(',', $arr_size[$row['goods_id']]);	
	}
	return $goods_list;
}

function create_pcs_confirm_order($pcs_order_id, $order_sn){
    $sql = "INSERT INTO " . $GLOBALS['os']->table('purchase_confirm_order') . "(order_sn, supplier_id, depot_id, " . 
    	   "seat_id, discount, deliver_time, goods_number, order_amount, order_status, order_brief, add_time) " . 
    	   "SELECT '$order_sn', supplier_id, depot_id, seat_id, discount, deliver_time, goods_number, order_amount, " . 
    	   "0, order_brief, " . gmtime() . " FROM " . $GLOBALS['os']->table('purchase_order') . " WHERE order_id=" . 
    	   intval($pcs_order_id);
    $res = $GLOBALS['db']->query($sql);	
    
    if ($res !== false)
    {    
    	$insert_id = $GLOBALS['db']->insert_id();
    	return $insert_id;
    }
    else
    {
        return false;
    } 
}

function create_pcs_confirm_order_goods($order_id, $pcs_order_id)
{
    $sql = "INSERT INTO " . $GLOBALS['os']->table('purchase_confirm_order_goods') . "(order_id, goods_id, color_id, " . 
    	   "size_id, goods_number, norm_price, discount, actual_price, norm_amount, actual_mount, goods_brief) " . 
    	   "SELECT " . intval($order_id) . ", goods_id, color_id, size_id, goods_number, norm_price, discount, " .
    	   "actual_price, norm_amount, actual_mount, goods_brief FROM " . $GLOBALS['os']->table('purchase_order_goods') . 
    	   " WHERE order_id=" . intval($pcs_order_id);
    $res = $GLOBALS['db']->query($sql);
    
    if ($res !== false)
    {    
    	return true;
    }
    else
    {
        return false;
    } 
}

function scm_enter_order_list(){
    $result = get_filter();
    if ($result === false)
    {
        /* 初始化分页参数 */
        $filter = array();
        $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'order_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
        $filter['order_sn'] = empty($_REQUEST['order_sn']) ? '' : trim($_REQUEST['order_sn']);
        $filter['status']     = (!is_numeric($_REQUEST['status']) || $_REQUEST['status']<0) ? -1 : intval($_REQUEST['status']);
        if (!empty($_GET['is_ajax']) && $_GET['is_ajax'] == 1)
        {
            $_REQUEST['supplier_name'] = json_str_iconv($_REQUEST['supplier_name']);
        }
        $filter['supplier_name'] = empty($_REQUEST['supplier_name']) ? '' : trim($_REQUEST['supplier_name']); 
        $filter['start_add_time'] = empty($_REQUEST['start_add_time']) ? '' : (strpos($_REQUEST['start_add_time'], '-') > 0 ?  local_strtotime($_REQUEST['start_add_time']) : $_REQUEST['start_add_time']);
        $filter['end_add_time'] = empty($_REQUEST['end_add_time']) ? '' : (strpos($_REQUEST['end_add_time'], '-') > 0 ?  local_strtotime($_REQUEST['end_add_time']) : $_REQUEST['end_add_time']);	
        $filter['start_deliver_time'] = empty($_REQUEST['start_deliver_time']) ? '' : (strpos($_REQUEST['start_deliver_time'], '-') > 0 ?  local_strtotime($_REQUEST['start_deliver_time']) : $_REQUEST['start_deliver_time']);
        $filter['end_deliver_time'] = empty($_REQUEST['end_deliver_time']) ? '' : (strpos($_REQUEST['end_deliver_time'], '-') > 0 ?  local_strtotime($_REQUEST['end_deliver_time']) : $_REQUEST['end_deliver_time']);

        $where = 'WHERE 1 ';
        if ($filter['order_sn'])
        {
            $where .= " AND o.order_sn LIKE '%" . mysql_like_quote($filter['order_sn']) . "%'";
        }
        if ($filter['supplier_name'])
        {
            $where .= " AND s.supplier_name LIKE '%" . mysql_like_quote($filter['supplier_name']) . "%'";
        } 
        if($filter['status']>=0){
        	$where .= " AND o.order_status=$filter[status]";
        } 
        if ($filter['start_add_time'])
        {
            $where .= " AND o.add_time >= '$filter[start_add_time]'";
        }
        if ($filter['end_add_time'])
        {
            $where .= " AND o.add_time <= '$filter[end_add_time]'";
        }
        if ($filter['start_deliver_time'])
        {
            $where .= " AND o.deliver_time >= '$filter[start_deliver_time]'";
        }
        if ($filter['end_deliver_time'])
        {
            $where .= " AND o.deliver_time <= '$filter[end_deliver_time]'";
        }                     
        /* 查询记录总数，计算分页数 */
   		$sql = "SELECT COUNT(*) FROM " . $GLOBALS['os']->table('purchase_confirm_order') . " o LEFT JOIN " . 
   		       $GLOBALS['os']->table('supplier') . " s ON o.supplier_id=s.supplier_id " . $where;
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);
        $filter = page_and_size($filter);

        /* 查询记录 */
        $sql = "SELECT o.*,s.supplier_name FROM " . $GLOBALS['os']->table('purchase_confirm_order') . " o LEFT JOIN " . 
   		       $GLOBALS['os']->table('supplier') . " s ON o.supplier_id=s.supplier_id " . $where .
   		       " ORDER BY $filter[sort_by] $filter[sort_order]";
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
		$row['status']  				    = jsc_order_status('enter', $row['order_status']);
		$row['formated_discount']  		    = sprintf('%s', floatval($row['discount']));
      	$row['formated_order_amount']	    = price_format($row['order_amount']);
      	$row['formated_add_time']           = local_date('Y-m-d', $row['add_time']);
      	$row['formated_deliver_time']       = local_date('Y-m-d', $row['deliver_time']);
      	$row['short_supplier_name']         = sub_str($row['supplier_name'], 10);
        $arr[] = $row;
    }
    return array('item' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);	
}

function pcs_confirm_order_info($order_id, $order_sn = ''){
  /* 计算订单各种费用之和的语句 */
    $order_id = intval($order_id);
    if ($order_id > 0)
    {
        $sql = 'SELECT o.*,s.supplier_name FROM ' . $GLOBALS['os']->table('purchase_confirm_order') . 
               ' o LEFT JOIN ' . $GLOBALS['os']->table('supplier') . ' s ON o.supplier_id=s.supplier_id ' . 
               " WHERE o.order_id = '$order_id'";	
    }
    else
    {
        $sql = 'SELECT o.*,s.supplier_name FROM ' . $GLOBALS['os']->table('purchase_confirm_order') . 
        	   ' o LEFT JOIN ' . $GLOBALS['os']->table('supplier') . ' s ON o.supplier_id=s.supplier_id ' . 		
               " WHERE o.order_sn = '$order_sn'";	
    }
    $order = $GLOBALS['db']->getRow($sql);

    /* 格式化金额字段 */
    if ($order)
    {
    	$order['order_sn']   	              = substr($order['order_sn'], 0);
    	$order['supplier_name']   	  	      = substr($order['supplier_name'], 0);
        $order['depot_name']                  = ' 默认 ';
        $order['seat_name']                   = ' 默认 ';
        $order['discount']                    = floatval($order['discount']);
        $order['formated_discount']           = sprintf('%s', floatval($order['discount']));
     	$order['formated_add_time']           = local_date('Y-m-d', $order['add_time']); 
     	$order['formated_deliver_time']       = local_date('Y-m-d', $order['deliver_time']);       
        $order['formated_order_amount']       = price_format(abs($order['order_amount']), false);
		$order['status']                      = jsc_order_status('enter', $order['order_status']);
    }

    return $order;	
}
function pcs_confirm_order_goods($order_id){	
	static $goods_list = array();
	$sql = "SELECT og.*,SUM(og.goods_number) subtotal_number,SUM(og.actual_mount) subtotal_mount,g.goods_name," . 
		   "g.goods_sn FROM " . $GLOBALS['os']->table('purchase_confirm_order_goods') . " og LEFT JOIN " . 
		   $GLOBALS['os']->table('goods') . " g ON og.goods_id=g.goods_id WHERE og.order_id=" . intval($order_id) . 
	       " GROUP BY og.goods_id ORDER BY rec_id DESC";
	$res = $GLOBALS['db']->query($sql);
	while ($row = $GLOBALS['db']->fetchRow($res)) {
		$goods_list[$row['goods_id']]['order_id']                =  $row['order_id'];
		$goods_list[$row['goods_id']]['goods_id']                =  $row['goods_id'];
		$goods_list[$row['goods_id']]['goods_name']              =  substr($row['goods_name'], 0);
		$goods_list[$row['goods_id']]['goods_sn']                =  substr($row['goods_sn'], 0);
		$goods_list[$row['goods_id']]['formated_norm_price']     =  price_format($row['norm_price']);		
		$goods_list[$row['goods_id']]['formated_discount']       =  sprintf('%s', floatval($row['discount']));
		$goods_list[$row['goods_id']]['formated_actual_price']   =  price_format($row['actual_price']);		  
		$goods_list[$row['goods_id']]['subtotal_number']         =  intval($row['subtotal_number']);
		$goods_list[$row['goods_id']]['formated_subtotal_mount'] =  price_format($row['subtotal_mount']);		      		
	}
	$sql = "SELECT og.*,g.goods_name,g.goods_sn,IFNULL(c.color_name,'默认') color_name,IFNULL(s.size_name,'默认') size_name,
	       IFNULL(b.brand_name, '') AS brand_name FROM " . $GLOBALS['os']->table('purchase_confirm_order_goods') . 
	       ' og LEFT JOIN ' . $GLOBALS['os']->table('goods') . ' g ON og.goods_id=g.goods_id LEFT JOIN ' . 
	       $GLOBALS['os']->table('color') . " c ON og.color_id=c.color_id LEFT JOIN " . $GLOBALS['os']->table('size') . 
	       ' s ON og.size_id=s.size_id LEFT JOIN ' . $GLOBALS['os']->table('brand') . ' b ON  g.brand_id=b.brand_id ' . 
	       ' WHERE og.order_id=' . intval($order_id) . ' ORDER BY og.rec_id DESC';
	$result = $GLOBALS['db']->query($sql);
	$arr_color = array();
	$arr_size = array();
	while ($row = $GLOBALS['db']->fetchRow($result)) {
		if(isset($goods_list[$row['goods_id']]) && is_array($goods_list[$row['goods_id']])){
			if(!is_array($arr_color[$row['goods_id']]) || !in_array($row['color_name'], $arr_color[$row['goods_id']])) 
					$arr_color[$row['goods_id']][]=$row['color_name'];
			if(!is_array($arr_size[$row['goods_id']]) || !in_array($row['size_name'], $arr_size[$row['goods_id']])) 
					$arr_size[$row['goods_id']][]=$row['size_name'];			
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['order_id']                =  $row['order_id'];
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['goods_id']                =  $row['goods_id'];
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['color_id']                =  $row['color_id'];
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['size_id']                 =  $row['size_id'];
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['goods_name']              =  substr($row['goods_name'], 0);
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['goods_sn']                =  substr($row['goods_sn'], 0);
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['formated_norm_price']     =  price_format($row['norm_price']);		
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['formated_discount']       =  sprintf('%s', floatval($row['discount']));
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['formated_actual_price']   =  price_format($row['actual_price']);		  
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['subtotal_number']         =  intval($row['goods_number']);
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['formated_subtotal_mount'] =  price_format(($row['actual_price'] * $row['goods_number']));	
		    $goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['goods_attr']              =  '颜色:' . $row['color_name'] . '&nbsp;&nbsp;尺码:' . $row['size_name'];
		}	
	    $goods_list[$row['goods_id']]['goods_attr']                                              =  '颜色:' . implode(',', $arr_color[$row['goods_id']]) . '&nbsp;&nbsp;尺码:' . implode(',', $arr_size[$row['goods_id']]);	
	}
	return $goods_list;
}


function pcs_balance_order_info($order_id){
    /* 计算订单各种费用之和的语句 */
    $order_id = intval($order_id);
    if ($order_id > 0)
    {
        $sql = 'SELECT o.*,s.supplier_name,p.pay_name FROM ' . $GLOBALS['os']->table('purchase_balance_order') . 
               ' o LEFT JOIN ' . $GLOBALS['os']->table('supplier') . ' s ON o.supplier_id=s.supplier_id ' . 
               ' LEFT JOIN ' . $GLOBALS['os']->table('purchase_payment') . ' p ON o.pay_type=p.pay_id ' .	
               " WHERE o.order_id = '$order_id'";	
    }
    else
    {
        $sql = 'SELECT o.*,s.supplier_name,p.pay_name FROM ' . $GLOBALS['os']->table('purchase_balance_order') . 
        	   ' o LEFT JOIN ' . $GLOBALS['os']->table('supplier') . ' s ON o.supplier_id=s.supplier_id ' .
        	   ' LEFT JOIN ' . $GLOBALS['os']->table('purchase_payment') . ' p ON o.pay_type=p.pay_id ' .	
               " WHERE o.order_sn = '$order_sn'";	
    }
    $order = $GLOBALS['db']->getRow($sql);

    /* 格式化金额字段 */
    if ($order)
    {
    	$order['order_sn']   	          = sub_str($order['order_sn'], 0);
    	$order['supplier_name']   	  	  = sub_str($order['supplier_name'], 0);
    	$order['pay_name']   	  	  	  = sub_str($order['pay_name'], 0);
     	$order['formated_add_time']       = local_date('Y-m-d', $order['add_time']);       
     	$order['formated_deliver_time']   = local_date('Y-m-d', $order['deliver_time']);       
        $order['formated_order_amount']   = price_format(abs($order['order_amount']), false);
        $order['formated_pay_amount']     = price_format(abs($order['pay_amount']), false);
        $order['formated_surplus_amount'] = price_format(abs($order['surplus_amount']), false);
		$order['status']                  = jsc_order_status('balance', $order['order_status']);
    }

    return $order;	
}

function pcs_return_order_info($order_id){
    /* 计算订单各种费用之和的语句 */
    $order_id = intval($order_id);
    if ($order_id > 0)
    {
        $sql = 'SELECT o.*,s.supplier_name FROM ' . $GLOBALS['os']->table('purchase_return_order') . 
               ' o LEFT JOIN ' . $GLOBALS['os']->table('supplier') . ' s ON o.supplier_id=s.supplier_id ' . 
               " WHERE o.order_id = '$order_id'";	
    }
    else
    {
        $sql = 'SELECT o.*,s.supplier_name FROM ' . $GLOBALS['os']->table('purchase_return_order') . 
        	   ' o LEFT JOIN ' . $GLOBALS['os']->table('supplier') . ' s ON o.supplier_id=s.supplier_id ' . 		
               " WHERE o.order_sn = '$order_sn'";	
    }
    $order = $GLOBALS['db']->getRow($sql);

    /* 格式化字段 */
    if ($order)
    {
    	$order['order_sn']   	              = substr($order['order_sn'], 0);
    	$order['supplier_name']   	  	      = substr($order['supplier_name'], 0);
        $order['depot_name']                  = ' 默认 ';
        $order['seat_name']                   = ' 默认 ';
        $order['discount']                    = floatval($order['discount']);
        $order['formated_discount']           = sprintf('%s', floatval($order['discount']));
     	$order['formated_add_time']           = local_date('Y-m-d', $order['add_time']); 
     	$order['formated_deliver_time']       = local_date('Y-m-d', $order['deliver_time']);       
        $order['formated_order_amount']       = price_format(abs($order['order_amount']), false);
		$order['status']                      = jsc_order_status('return', $order['order_status']);
    }

    return $order;	
}

function pcs_return_order_goods($order_id){
	static $goods_list = array();
	$sql = "SELECT og.*,SUM(og.goods_number) subtotal_number,SUM(og.actual_mount) subtotal_mount,g.goods_name," . 
		   "g.goods_sn FROM " . $GLOBALS['os']->table('purchase_return_order_goods') . " og LEFT JOIN " . 
		   $GLOBALS['os']->table('goods') . " g ON og.goods_id=g.goods_id WHERE og.order_id=" . intval($order_id) . 
	       " GROUP BY og.goods_id ORDER BY rec_id DESC";
	$res = $GLOBALS['db']->query($sql);
	while ($row = $GLOBALS['db']->fetchRow($res)) {
		$goods_list[$row['goods_id']]['order_id']                =  $row['order_id'];
		$goods_list[$row['goods_id']]['goods_id']                =  $row['goods_id'];
		$goods_list[$row['goods_id']]['goods_name']              =  substr($row['goods_name'], 0);
		$goods_list[$row['goods_id']]['goods_sn']                =  substr($row['goods_sn'], 0);
		$goods_list[$row['goods_id']]['formated_norm_price']     =  price_format($row['norm_price']);		
		$goods_list[$row['goods_id']]['formated_discount']       =  sprintf('%s', floatval($row['discount']));
		$goods_list[$row['goods_id']]['formated_actual_price']   =  price_format($row['actual_price']);		  
		$goods_list[$row['goods_id']]['subtotal_number']         =  intval($row['subtotal_number']);
		$goods_list[$row['goods_id']]['formated_subtotal_mount'] =  price_format($row['subtotal_mount']);		      		
	}
	$sql = "SELECT og.*,g.goods_name,g.goods_sn,IFNULL(c.color_name,'默认') color_name,IFNULL(s.size_name,'默认') size_name,
	       IFNULL(b.brand_name, '') AS brand_name FROM " . $GLOBALS['os']->table('purchase_return_order_goods') . 
	       ' og LEFT JOIN ' . $GLOBALS['os']->table('goods') . ' g ON og.goods_id=g.goods_id LEFT JOIN ' . 
	       $GLOBALS['os']->table('color') . " c ON og.color_id=c.color_id LEFT JOIN " . $GLOBALS['os']->table('size') . 
	       ' s ON og.size_id=s.size_id LEFT JOIN ' . $GLOBALS['os']->table('brand') . ' b ON  g.brand_id=b.brand_id ' . 
	       ' WHERE og.order_id=' . intval($order_id) . ' ORDER BY og.rec_id DESC';
	$result = $GLOBALS['db']->query($sql);
	$arr_color = array();
	$arr_size = array();
	while ($row = $GLOBALS['db']->fetchRow($result)) {
		if(isset($goods_list[$row['goods_id']]) && is_array($goods_list[$row['goods_id']])){
			if(!is_array($arr_color[$row['goods_id']]) || !in_array($row['color_name'], $arr_color[$row['goods_id']])) 
					$arr_color[$row['goods_id']][]=$row['color_name'];
			if(!is_array($arr_size[$row['goods_id']]) || !in_array($row['size_name'], $arr_size[$row['goods_id']])) 
					$arr_size[$row['goods_id']][]=$row['size_name'];			
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['order_id']                =  $row['order_id'];
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['goods_id']                =  $row['goods_id'];
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['color_id']                =  $row['color_id'];
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['size_id']                 =  $row['size_id'];
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['goods_name']              =  substr($row['goods_name'], 0);
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['goods_sn']                =  substr($row['goods_sn'], 0);
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['formated_norm_price']     =  price_format($row['norm_price']);		
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['formated_discount']       =  sprintf('%s', floatval($row['discount']));
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['formated_actual_price']   =  price_format($row['actual_price']);		  
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['subtotal_number']         =  intval($row['goods_number']);
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['formated_subtotal_mount'] =  price_format(($row['actual_price'] * $row['goods_number']));	
		    $goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['goods_attr']              =  '颜色:' . $row['color_name'] . '&nbsp;&nbsp;尺码:' . $row['size_name'];
		}	
	    $goods_list[$row['goods_id']]['goods_attr']                                              =  '颜色:' . implode(',', $arr_color[$row['goods_id']]) . '&nbsp;&nbsp;尺码:' . implode(',', $arr_size[$row['goods_id']]);	
	}
	return $goods_list;	
}


function pcs_return_order_list(){
    $result = get_filter();
    if ($result === false)
    {
        /* 初始化分页参数 */
        $filter = array();
        $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'order_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
        $filter['order_sn']   = empty($_REQUEST['order_sn']) ? '' : trim($_REQUEST['order_sn']);
        $filter['status']     = (!is_numeric($_REQUEST['status']) || $_REQUEST['status']<0) ? -1 : intval($_REQUEST['status']);
        if (!empty($_GET['is_ajax']) && $_GET['is_ajax'] == 1)
        {
            $_REQUEST['supplier_name'] = json_str_iconv($_REQUEST['supplier_name']);
        }
        $filter['supplier_name']       = empty($_REQUEST['supplier_name']) ? '' : trim($_REQUEST['supplier_name']); 
        $filter['start_add_time']      = empty($_REQUEST['start_add_time']) ? '' : (strpos($_REQUEST['start_add_time'], '-') > 0 ?  local_strtotime($_REQUEST['start_add_time']) : $_REQUEST['start_add_time']);
        $filter['end_add_time']        = empty($_REQUEST['end_add_time']) ? '' : (strpos($_REQUEST['end_add_time'], '-') > 0 ?  local_strtotime($_REQUEST['end_add_time']) : $_REQUEST['end_add_time']);	
        $filter['start_deliver_time']  = empty($_REQUEST['start_deliver_time']) ? '' : (strpos($_REQUEST['start_deliver_time'], '-') > 0 ?  local_strtotime($_REQUEST['start_deliver_time']) : $_REQUEST['start_deliver_time']);
        $filter['end_deliver_time']    = empty($_REQUEST['end_deliver_time']) ? '' : (strpos($_REQUEST['end_deliver_time'], '-') > 0 ?  local_strtotime($_REQUEST['end_deliver_time']) : $_REQUEST['end_deliver_time']);

        $where = 'WHERE 1 ';
        if ($filter['order_sn'])
        {
            $where .= " AND o.order_sn LIKE '%" . mysql_like_quote($filter['order_sn']) . "%'";
        }
        if($filter['status']>=0){
        	$where .= " AND o.order_status=$filter[status]";
        }
        if ($filter['supplier_name'])
        {
            $where .= " AND o.supplier_name LIKE '%" . mysql_like_quote($filter['supplier_name']) . "%'";
        }  
        if ($filter['start_add_time'])
        {
            $where .= " AND o.add_time >= '$filter[start_add_time]'";
        }
        if ($filter['end_add_time'])
        {
            $where .= " AND o.add_time <= '$filter[end_add_time]'";
        }
        if ($filter['start_deliver_time'])
        {
            $where .= " AND o.deliver_time >= '$filter[start_deliver_time]'";
        }
        if ($filter['end_deliver_time'])
        {
            $where .= " AND o.deliver_time <= '$filter[end_deliver_time]'";
        }                     
        /* 查询记录总数，计算分页数 */
   		$sql = "SELECT COUNT(*) FROM " . $GLOBALS['os']->table('purchase_return_order') . " o LEFT JOIN " . 
   		       $GLOBALS['os']->table('supplier') . " s ON o.supplier_id=s.supplier_id " . $where;
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);
        $filter = page_and_size($filter);

        /* 查询记录 */
        $sql = "SELECT o.*,s.supplier_name FROM " . $GLOBALS['os']->table('purchase_return_order') . " o LEFT JOIN " . 
   		       $GLOBALS['os']->table('supplier') . " s ON o.supplier_id=s.supplier_id " . $where .
   		       " ORDER BY $filter[sort_by] $filter[sort_order]";
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
		$row['status']  				    = jsc_order_status('return', $row['order_status']);
		$row['formated_discount']  		    = sprintf('%s', floatval($row['discount']));
      	$row['formated_order_amount']	    = price_format($row['order_amount']);
      	$row['formated_add_time']           = local_date('Y-m-d', $row['add_time']);
      	$row['formated_deliver_time']       = local_date('Y-m-d', $row['deliver_time']);
      	$row['short_supplier_name']         = sub_str($row['supplier_name'], 10);
        $arr[] = $row;
    }
    return array('item' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);	
}

function pcs_check_order_info($order_id, $order_sn = ''){
  /* 计算订单各种费用之和的语句 */
    $order_id = intval($order_id);
    if ($order_id > 0)
    {
        $sql = "SELECT o.* FROM " . $GLOBALS['os']->table('purchase_check_order') . " o WHERE o.order_id = '$order_id'";	
    }
    else
    {
        $sql = "SELECT o.* FROM " . $GLOBALS['os']->table('purchase_check_order') . " o WHERE o.order_sn = '$order_sn'";	
    }
    $order = $GLOBALS['db']->getRow($sql);

    /* 格式化金额字段 */
    if ($order)
    {		
    	$order['order_sn']   	              = substr($order['order_sn'], 0);
        $order['depot_name']                  = ' 默认 ';
        $order['seat_name']                   = ' 默认 ';
        $order['discount']                    = floatval($order['discount']);
        $order['formated_discount']           = sprintf('%s', floatval($order['discount']));
     	$order['formated_add_time']           = local_date('Y-m-d', $order['add_time']);     
        $order['formated_order_amount']       = price_format(abs($order['order_amount']), false);
		$order['status']                      = jsc_order_status('check', $order['order_status']);
    }

    return $order;	
}

function pcs_check_order_goods($order_id){
	static $goods_list = array();
	$sql = "SELECT og.*,SUM(og.goods_number) subtotal_number,SUM(og.actual_mount) subtotal_mount,g.goods_name," . 
		   "g.goods_sn FROM " . $GLOBALS['os']->table('purchase_check_order_goods') . " og LEFT JOIN " . 
		   $GLOBALS['os']->table('goods') . " g ON og.goods_id=g.goods_id WHERE og.order_id=" . intval($order_id) . 
	       " GROUP BY og.goods_id ORDER BY rec_id DESC";
	$res = $GLOBALS['db']->query($sql);
	while ($row = $GLOBALS['db']->fetchRow($res)) {
		$goods_list[$row['goods_id']]['order_id']                =  $row['order_id'];
		$goods_list[$row['goods_id']]['goods_id']                =  $row['goods_id'];
		$goods_list[$row['goods_id']]['goods_name']              =  substr($row['goods_name'], 0);
		$goods_list[$row['goods_id']]['goods_sn']                =  substr($row['goods_sn'], 0);
		$goods_list[$row['goods_id']]['formated_norm_price']     =  price_format($row['norm_price']);		
		$goods_list[$row['goods_id']]['formated_discount']       =  sprintf('%s', floatval($row['discount']));
		$goods_list[$row['goods_id']]['formated_actual_price']   =  price_format($row['actual_price']);		  
		$goods_list[$row['goods_id']]['subtotal_number']         =  intval($row['subtotal_number']);
		$goods_list[$row['goods_id']]['formated_subtotal_mount'] =  price_format($row['subtotal_mount']);		      		
	}
	$sql = "SELECT og.*,g.goods_name,g.goods_sn,IFNULL(c.color_name,'默认') color_name,IFNULL(s.size_name,'默认') size_name,
	       IFNULL(b.brand_name, '') AS brand_name FROM " . $GLOBALS['os']->table('purchase_check_order_goods') . 
	       ' og LEFT JOIN ' . $GLOBALS['os']->table('goods') . ' g ON og.goods_id=g.goods_id LEFT JOIN ' . 
	       $GLOBALS['os']->table('color') . " c ON og.color_id=c.color_id LEFT JOIN " . $GLOBALS['os']->table('size') . 
	       ' s ON og.size_id=s.size_id LEFT JOIN ' . $GLOBALS['os']->table('brand') . ' b ON  g.brand_id=b.brand_id ' . 
	       ' WHERE og.order_id=' . intval($order_id) . ' ORDER BY og.rec_id DESC';
	$result = $GLOBALS['db']->query($sql);
	$arr_color = array();
	$arr_size = array();
	while ($row = $GLOBALS['db']->fetchRow($result)) {
		if(isset($goods_list[$row['goods_id']]) && is_array($goods_list[$row['goods_id']])){
			if(!is_array($arr_color[$row['goods_id']]) || !in_array($row['color_name'], $arr_color[$row['goods_id']])) 
					$arr_color[$row['goods_id']][]=$row['color_name'];
			if(!is_array($arr_size[$row['goods_id']]) || !in_array($row['size_name'], $arr_size[$row['goods_id']])) 
					$arr_size[$row['goods_id']][]=$row['size_name'];			
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['order_id']                =  $row['order_id'];
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['goods_id']                =  $row['goods_id'];
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['color_id']                =  $row['color_id'];
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['size_id']                 =  $row['size_id'];
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['goods_name']              =  substr($row['goods_name'], 0);
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['goods_sn']                =  substr($row['goods_sn'], 0);
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['formated_norm_price']     =  price_format($row['norm_price']);		
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['formated_discount']       =  sprintf('%s', floatval($row['discount']));
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['formated_actual_price']   =  price_format($row['actual_price']);		  
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['subtotal_number']         =  intval($row['goods_number']);
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['formated_subtotal_mount'] =  price_format(($row['actual_price'] * $row['goods_number']));	
		    $goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['goods_attr']              =  '颜色:' . $row['color_name'] . '&nbsp;&nbsp;尺码:' . $row['size_name'];
		}	
	    $goods_list[$row['goods_id']]['goods_attr']                                              =  '颜色:' . implode(',', $arr_color[$row['goods_id']]) . '&nbsp;&nbsp;尺码:' . implode(',', $arr_size[$row['goods_id']]);	
	}
	return $goods_list;
}

function pcs_check_order_list(){
    $result = get_filter();
    if ($result === false)
    {
        /* 初始化分页参数 */
        $filter = array();
        $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'order_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
        $filter['order_sn'] = empty($_REQUEST['order_sn']) ? '' : trim($_REQUEST['order_sn']);
        $filter['status']     = (!is_numeric($_REQUEST['status']) || $_REQUEST['status']<0) ? -1 : intval($_REQUEST['status']);
        $filter['start_add_time'] = empty($_REQUEST['start_add_time']) ? '' : (strpos($_REQUEST['start_add_time'], '-') > 0 ?  local_strtotime($_REQUEST['start_add_time']) : $_REQUEST['start_add_time']);
        $filter['end_add_time'] = empty($_REQUEST['end_add_time']) ? '' : (strpos($_REQUEST['end_add_time'], '-') > 0 ?  local_strtotime($_REQUEST['end_add_time']) : $_REQUEST['end_add_time']);	

        $where = 'WHERE 1 ';
        if ($filter['order_sn'])
        {
            $where .= " AND o.order_sn LIKE '%" . mysql_like_quote($filter['order_sn']) . "%'";
        }
        if($filter['status']>=0){
        	$where .= " AND o.order_status=$filter[status]";
        } 
        if ($filter['start_add_time'])
        {
            $where .= " AND o.add_time >= '$filter[start_add_time]'";
        }
        if ($filter['end_add_time'])
        {
            $where .= " AND o.add_time <= '$filter[end_add_time]'";
        }                   
        /* 查询记录总数，计算分页数 */
   		$sql = "SELECT COUNT(*) FROM " . $GLOBALS['os']->table('purchase_check_order') . " o " . $where;
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);
        $filter = page_and_size($filter);

        /* 查询记录 */
        $sql = "SELECT o.* FROM " . $GLOBALS['os']->table('purchase_check_order') . " o " . $where .
   		       " ORDER BY $filter[sort_by] $filter[sort_order]";
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
		$row['status']  				    = jsc_order_status('check', $row['order_status']);
		$row['formated_discount']  		    = sprintf('%s', floatval($row['discount']));
      	$row['formated_order_amount']	    = price_format($row['order_amount']);
      	$row['formated_add_time']           = local_date('Y-m-d', $row['add_time']);
        $arr[] = $row;
    }
    return array('item' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);	
}

/////
function pcs_loss_order_info($order_id, $order_sn = ''){
    /* 计算订单各种费用之和的语句 */
    $order_id = intval($order_id);
    if ($order_id > 0)
    {
        $sql = "SELECT o.* FROM " . $GLOBALS['os']->table('purchase_loss_order') . " o WHERE o.order_id = '$order_id'";	
    }
    else
    {
        $sql = "SELECT o.* FROM " . $GLOBALS['os']->table('purchase_loss_order') . " o WHERE o.order_sn = '$order_sn'";	
    }
    $order = $GLOBALS['db']->getRow($sql);

    /* 格式化金额字段 */
    if ($order)
    {
    	$order['order_sn']   	              = substr($order['order_sn'], 0);
        $order['depot_name']                  = ' 默认 ';
        $order['seat_name']                   = ' 默认 ';
        $order['discount']                    = floatval($order['discount']);
        $order['formated_discount']           = sprintf('%s', floatval($order['discount']));
     	$order['formated_add_time']           = local_date('Y-m-d', $order['add_time']);     
        $order['formated_order_amount']       = price_format(abs($order['order_amount']), false);
		$order['status']                      = jsc_order_status('loss', $order['order_status']);
    }

    return $order;	
}

function pcs_loss_order_goods($order_id){
	static $goods_list = array();
	$sql = "SELECT og.*,SUM(og.goods_number) subtotal_number,SUM(og.actual_mount) subtotal_mount,g.goods_name," . 
		   "g.goods_sn FROM " . $GLOBALS['os']->table('purchase_loss_order_goods') . " og LEFT JOIN " . 
		   $GLOBALS['os']->table('goods') . " g ON og.goods_id=g.goods_id WHERE og.order_id=" . intval($order_id) . 
	       " GROUP BY og.goods_id ORDER BY rec_id DESC";
	$res = $GLOBALS['db']->query($sql);
	while ($row = $GLOBALS['db']->fetchRow($res)) {
		$goods_list[$row['goods_id']]['order_id']                =  $row['order_id'];
		$goods_list[$row['goods_id']]['goods_id']                =  $row['goods_id'];
		$goods_list[$row['goods_id']]['goods_name']              =  substr($row['goods_name'], 0);
		$goods_list[$row['goods_id']]['goods_sn']                =  substr($row['goods_sn'], 0);
		$goods_list[$row['goods_id']]['formated_norm_price']     =  price_format($row['norm_price']);		
		$goods_list[$row['goods_id']]['formated_discount']       =  sprintf('%s', floatval($row['discount']));
		$goods_list[$row['goods_id']]['formated_actual_price']   =  price_format($row['actual_price']);		  
		$goods_list[$row['goods_id']]['subtotal_number']         =  intval($row['subtotal_number']);
		$goods_list[$row['goods_id']]['formated_subtotal_mount'] =  price_format($row['subtotal_mount']);		      		
	}
	$sql = "SELECT og.*,g.goods_name,g.goods_sn,IFNULL(c.color_name,'默认') color_name,IFNULL(s.size_name,'默认') size_name,
	       IFNULL(b.brand_name, '') AS brand_name FROM " . $GLOBALS['os']->table('purchase_loss_order_goods') . 
	       ' og LEFT JOIN ' . $GLOBALS['os']->table('goods') . ' g ON og.goods_id=g.goods_id LEFT JOIN ' . 
	       $GLOBALS['os']->table('color') . " c ON og.color_id=c.color_id LEFT JOIN " . $GLOBALS['os']->table('size') . 
	       ' s ON og.size_id=s.size_id LEFT JOIN ' . $GLOBALS['os']->table('brand') . ' b ON  g.brand_id=b.brand_id ' . 
	       ' WHERE og.order_id=' . intval($order_id) . ' ORDER BY og.rec_id DESC';
	$result = $GLOBALS['db']->query($sql);
	$arr_color = array();
	$arr_size = array();
	while ($row = $GLOBALS['db']->fetchRow($result)) {
		if(isset($goods_list[$row['goods_id']]) && is_array($goods_list[$row['goods_id']])){
			if(!is_array($arr_color[$row['goods_id']]) || !in_array($row['color_name'], $arr_color[$row['goods_id']])) 
					$arr_color[$row['goods_id']][]=$row['color_name'];
			if(!is_array($arr_size[$row['goods_id']]) || !in_array($row['size_name'], $arr_size[$row['goods_id']])) 
					$arr_size[$row['goods_id']][]=$row['size_name'];			
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['order_id']                =  $row['order_id'];
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['goods_id']                =  $row['goods_id'];
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['color_id']                =  $row['color_id'];
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['size_id']                 =  $row['size_id'];
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['goods_name']              =  substr($row['goods_name'], 0);
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['goods_sn']                =  substr($row['goods_sn'], 0);
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['formated_norm_price']     =  price_format($row['norm_price']);		
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['formated_discount']       =  sprintf('%s', floatval($row['discount']));
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['formated_actual_price']   =  price_format($row['actual_price']);		  
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['subtotal_number']         =  intval($row['goods_number']);
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['formated_subtotal_mount'] =  price_format(($row['actual_price'] * $row['goods_number']));	
		    $goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['goods_attr']              =  '颜色:' . $row['color_name'] . '&nbsp;&nbsp;尺码:' . $row['size_name'];
		}	
	    $goods_list[$row['goods_id']]['goods_attr']                                              =  '颜色:' . implode(',', $arr_color[$row['goods_id']]) . '&nbsp;&nbsp;尺码:' . implode(',', $arr_size[$row['goods_id']]);	
	}
	return $goods_list;
}

function pcs_loss_order_list(){
    $result = get_filter();
    if ($result === false)
    {
        /* 初始化分页参数 */
        $filter = array();
        $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'order_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
        $filter['order_sn'] = empty($_REQUEST['order_sn']) ? '' : trim($_REQUEST['order_sn']);
        $filter['status']     = (!is_numeric($_REQUEST['status']) || $_REQUEST['status']<0) ? -1 : intval($_REQUEST['status']);
        $filter['start_add_time'] = empty($_REQUEST['start_add_time']) ? '' : (strpos($_REQUEST['start_add_time'], '-') > 0 ?  local_strtotime($_REQUEST['start_add_time']) : $_REQUEST['start_add_time']);
        $filter['end_add_time'] = empty($_REQUEST['end_add_time']) ? '' : (strpos($_REQUEST['end_add_time'], '-') > 0 ?  local_strtotime($_REQUEST['end_add_time']) : $_REQUEST['end_add_time']);	

        $where = 'WHERE 1 ';
        if ($filter['order_sn'])
        {
            $where .= " AND o.order_sn LIKE '%" . mysql_like_quote($filter['order_sn']) . "%'";
        }
        if($filter['status']>=0){
        	$where .= " AND o.order_status=$filter[status]";
        } 
        if ($filter['start_add_time'])
        {
            $where .= " AND o.add_time >= '$filter[start_add_time]'";
        }
        if ($filter['end_add_time'])
        {
            $where .= " AND o.add_time <= '$filter[end_add_time]'";
        }                   
        /* 查询记录总数，计算分页数 */
   		$sql = "SELECT COUNT(*) FROM " . $GLOBALS['os']->table('purchase_loss_order') . " o " . $where;
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);
        $filter = page_and_size($filter);

        /* 查询记录 */
        $sql = "SELECT o.* FROM " . $GLOBALS['os']->table('purchase_loss_order') . " o " . $where .
   		       " ORDER BY $filter[sort_by] $filter[sort_order]";
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {      
		$row['status']  				    = jsc_order_status('loss', $row['order_status']);
		$row['formated_discount']  		    = sprintf('%s', floatval($row['discount']));
      	$row['formated_order_amount']	    = price_format($row['order_amount']);
      	$row['formated_add_time']           = local_date('Y-m-d', $row['add_time']);
        $arr[] = $row;
    }
    return array('item' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);	
}

function pcs_rectify_order_list(){
    $result = get_filter();
    if ($result === false)
    {
        /* 初始化分页参数 */
        $filter = array();
        $filter['sort_by']    		= empty($_REQUEST['sort_by']) ? 'order_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] 		= empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
        $filter['order_sn'] 		= empty($_REQUEST['order_sn']) ? '' : trim($_REQUEST['order_sn']);
        $filter['status']     		= (!is_numeric($_REQUEST['status']) || $_REQUEST['status']<0) ? -1 : intval($_REQUEST['status']);
        $filter['start_add_time']	= empty($_REQUEST['start_add_time']) ? '' : (strpos($_REQUEST['start_add_time'], '-') > 0 ?  local_strtotime($_REQUEST['start_add_time']) : $_REQUEST['start_add_time']);
        $filter['end_add_time'] 	= empty($_REQUEST['end_add_time']) ? '' : (strpos($_REQUEST['end_add_time'], '-') > 0 ?  local_strtotime($_REQUEST['end_add_time']) : $_REQUEST['end_add_time']);	

        $where = 'WHERE 1 ';
        if ($filter['order_sn'])
        {
            $where .= " AND o.order_sn LIKE '%" . mysql_like_quote($filter['order_sn']) . "%'";
        }
        if($filter['status']>=0){
        	$where .= " AND o.order_status=$filter[status]";
        } 
        if ($filter['start_add_time'])
        {
            $where .= " AND o.add_time >= '$filter[start_add_time]'";
        }
        if ($filter['end_add_time'])
        {
            $where .= " AND o.add_time <= '$filter[end_add_time]'";
        }                   
        /* 查询记录总数，计算分页数 */
   		$sql = "SELECT COUNT(*) FROM " . $GLOBALS['os']->table('purchase_rectify_order') . " o " . $where;
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);
        $filter = page_and_size($filter);

        /* 查询记录 */
        $sql = "SELECT o.* FROM " . $GLOBALS['os']->table('purchase_rectify_order') . " o " . $where .
   		       " ORDER BY $filter[sort_by] $filter[sort_order]";
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
		$row['status']  				    = jsc_order_status('rectify', $row['order_status']);
		$row['formated_discount']  		    = sprintf('%s', floatval($row['discount']));
      	$row['formated_order_amount']	    = price_format($row['order_amount']);
      	$row['formated_add_time']           = local_date('Y-m-d', $row['add_time']);
        $arr[] = $row;
    }
    return array('item' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);		
}

function pcs_rectify_order_info($order_id, $order_sn = ''){
    /* 计算订单各种费用之和的语句 */
    $order_id = intval($order_id);
    if ($order_id > 0)
    {
        $sql = "SELECT o.* FROM " . $GLOBALS['os']->table('purchase_rectify_order') . " o WHERE o.order_id = '$order_id'";	
    }
    else
    {
        $sql = "SELECT o.* FROM " . $GLOBALS['os']->table('purchase_rectify_order') . " o WHERE o.order_sn = '$order_sn'";	
    }
    $order = $GLOBALS['db']->getRow($sql);

    /* 格式化金额字段 */
    if ($order)
    {		
    	$order['order_sn']   	              = substr($order['order_sn'], 0);
        $order['depot_name']                  = ' 默认 ';
        $order['seat_name']                   = ' 默认 ';
        $order['discount']                    = floatval($order['discount']);
        $order['formated_discount']           = sprintf('%s', floatval($order['discount']));
     	$order['formated_add_time']           = local_date('Y-m-d', $order['add_time']);     
        $order['formated_order_amount']       = price_format(abs($order['order_amount']), false);
		$order['status']                      = jsc_order_status('rectify', $order['order_status']);
    }

    return $order;		
}

function pcs_rectify_order_goods($order_id){
	static $goods_list = array();
	$sql = "SELECT og.*,SUM(og.goods_number) subtotal_number,SUM(og.actual_mount) subtotal_mount,g.goods_name," . 
		   "g.goods_sn FROM " . $GLOBALS['os']->table('purchase_rectify_order_goods') . " og LEFT JOIN " . 
		   $GLOBALS['os']->table('goods') . " g ON og.goods_id=g.goods_id WHERE og.order_id=" . intval($order_id) . 
	       " GROUP BY og.goods_id ORDER BY rec_id DESC";
	$res = $GLOBALS['db']->query($sql);
	while ($row = $GLOBALS['db']->fetchRow($res)) {
		$goods_list[$row['goods_id']]['order_id']                =  $row['order_id'];
		$goods_list[$row['goods_id']]['goods_id']                =  $row['goods_id'];
		$goods_list[$row['goods_id']]['goods_name']              =  substr($row['goods_name'], 0);
		$goods_list[$row['goods_id']]['goods_sn']                =  substr($row['goods_sn'], 0);
		$goods_list[$row['goods_id']]['formated_norm_price']     =  price_format($row['norm_price']);		
		$goods_list[$row['goods_id']]['formated_discount']       =  sprintf('%s', floatval($row['discount']));
		$goods_list[$row['goods_id']]['formated_actual_price']   =  price_format($row['actual_price']);		  
		$goods_list[$row['goods_id']]['subtotal_number']         =  intval($row['subtotal_number']);
		$goods_list[$row['goods_id']]['formated_subtotal_mount'] =  price_format($row['subtotal_mount']);		      		
	}
	$sql = "SELECT og.*,g.goods_name,g.goods_sn,IFNULL(c.color_name,'默认') color_name,IFNULL(s.size_name,'默认') size_name,
	       IFNULL(b.brand_name, '') AS brand_name FROM " . $GLOBALS['os']->table('purchase_rectify_order_goods') . 
	       ' og LEFT JOIN ' . $GLOBALS['os']->table('goods') . ' g ON og.goods_id=g.goods_id LEFT JOIN ' . 
	       $GLOBALS['os']->table('color') . " c ON og.color_id=c.color_id LEFT JOIN " . $GLOBALS['os']->table('size') . 
	       ' s ON og.size_id=s.size_id LEFT JOIN ' . $GLOBALS['os']->table('brand') . ' b ON  g.brand_id=b.brand_id ' . 
	       ' WHERE og.order_id=' . intval($order_id) . ' ORDER BY og.rec_id DESC';
	$result = $GLOBALS['db']->query($sql);
	$arr_color = array();
	$arr_size = array();
	while ($row = $GLOBALS['db']->fetchRow($result)) {
		if(isset($goods_list[$row['goods_id']]) && is_array($goods_list[$row['goods_id']])){
			if(!is_array($arr_color[$row['goods_id']]) || !in_array($row['color_name'], $arr_color[$row['goods_id']])) 
					$arr_color[$row['goods_id']][]=$row['color_name'];
			if(!is_array($arr_size[$row['goods_id']]) || !in_array($row['size_name'], $arr_size[$row['goods_id']])) 
					$arr_size[$row['goods_id']][]=$row['size_name'];			
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['order_id']                =  $row['order_id'];
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['goods_id']                =  $row['goods_id'];
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['color_id']                =  $row['color_id'];
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['size_id']                 =  $row['size_id'];
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['goods_name']              =  substr($row['goods_name'], 0);
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['goods_sn']                =  substr($row['goods_sn'], 0);
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['formated_norm_price']     =  price_format($row['norm_price']);		
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['formated_discount']       =  sprintf('%s', floatval($row['discount']));
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['formated_actual_price']   =  price_format($row['actual_price']);		  
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['subtotal_number']         =  intval($row['goods_number']);
			$goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['formated_subtotal_mount'] =  price_format(($row['actual_price'] * $row['goods_number']));	
		    $goods_list[$row['goods_id']]['sub_list'][$row['rec_id']]['goods_attr']              =  '颜色:' . $row['color_name'] . '&nbsp;&nbsp;尺码:' . $row['size_name'];
		}	
	    $goods_list[$row['goods_id']]['goods_attr']                                              =  '颜色:' . implode(',', $arr_color[$row['goods_id']]) . '&nbsp;&nbsp;尺码:' . implode(',', $arr_size[$row['goods_id']]);	
	}
	return $goods_list;	
}

function alter_storage($type, $order_id, $is_plus=TRUE){
	switch ($type) {
		case 'confirm_order':
			$tb = 'purchase_confirm_order_goods';
			$sql = "SELECT goods_id,color_id,size_id,goods_number FROM ".$GLOBALS['os']->table($tb).
	       		   " WHERE order_id=" . intval($order_id);
			break;
		case 'return_order':
			$tb = 'purchase_return_order_goods';
			$sql = "SELECT goods_id,color_id,size_id,goods_number FROM ".$GLOBALS['os']->table($tb).
	               " WHERE order_id=" . intval($order_id);
			break;
		case 'rectify_order':
		    $tb = 'purchase_rectify_order_goods';
			$sql = "SELECT goods_id,color_id,size_id,goods_number_rectify AS goods_number FROM ".$GLOBALS['os']->table($tb).
	       		   " WHERE order_id=" . intval($order_id);
	        break;
		default:
			return false;exit;
			break;
	}
	$res = $GLOBALS['db']->query($sql);
	
    if ($res === FALSE)
    {    
        return false;    	
    }
    
	$sign = ($is_plus!==FALSE)  ?  '+'  :  '-';
	while ($row=$GLOBALS['db']->fetchRow($res)) {
		$rectify_num = !empty($row['goods_number']) ? intval($row['goods_number']) : 0;
    	$sql_in = "UPDATE " . $GLOBALS['os']->table('goods_inventory') . " SET goods_number_actual=goods_number_actual$sign".
    	          $rectify_num.",goods_number_available=goods_number_available$sign".$rectify_num.
    	          " WHERE goods_id=$row[goods_id] AND color_id=$row[color_id] AND size_id=$row[size_id]";
    	$GLOBALS['db']->query($sql_in);  				
	}
	return true;
}

function pcs_balance_order_item_list($order_id){
	$sql = "SELECT d.*,o.order_sn FROM " . $GLOBALS['os']->table('purchase_balance_order_detail') . 
	       " d LEFT JOIN " . $GLOBALS['os']->table('purchase_confirm_order') . " o ON o.order_id=d.order_id " . 
	       "WHERE d.order_id=" . intval($order_id) . " AND d.order_type=1 UNION SELECT d.*," . 
	       "o.order_sn FROM " . $GLOBALS['os']->table('purchase_balance_order_detail') . " d LEFT JOIN " . 
	       $GLOBALS['os']->table('purchase_return_order'). " o ON o.order_id=d.order_id WHERE d.order_id=" . 
	       intval($order_id) . " AND d.order_type=2 ORDER BY rec_id DESC";
	$res = $GLOBALS['db']->query($sql);
	$order_list = array();
	while ($row = $GLOBALS['db']->fetchRow($res)) {
		$order_list[$row['rec_id']]['order_id']                =  $row['order_id'];
		$order_list[$row['rec_id']]['order_sn']                =  $row['order_sn'];
		$order_list[$row['rec_id']]['order_type']              =  empty($row['order_type']) ? 0 : intval($row['order_type']);
		$order_list[$row['rec_id']]['formated_associate_time'] =  $row['associate_time'];
		$order_list[$row['rec_id']]['pay_amount']   		   =  $row['pay_amount'];			
		$order_list[$row['rec_id']]['formated_order_amount']   =  price_format($row['order_amount']);	
		$order_list[$row['rec_id']]['formated_payed_amount']   =  price_format($row['payed_amount']);		
		$order_list[$row['rec_id']]['formated_pay_amount']     =  price_format($row['pay_amount']);			
		$order_list[$row['rec_id']]['formated_surplus_amount'] =  price_format($row['surplus_amount']);	
		$order_list[$row['rec_id']]['formated_add_time']       =  local_date('Y-m-d', $row['add_time']);	      		
	}
	return $order_list;		
}

function jxc_order_amount($type, $order_id){
	switch ($type){
		default:return false; break;
		case 'pcs':
			$tb = 'purchase_order_goods';
			break;
		case 'enter':
			$tb = 'purchase_confirm_order_goods';
			break;
		case 'check':
			$tb = 'purchase_check_order_goods';
			break;
		case 'return':
			$tb = 'purchase_return_order_goods';			
			break;
	}
	$arr_amount = array();
	$sql = "SELECT goods_number,norm_price,discount,actual_price FROM " . 
	       $GLOBALS['os']->table($tb) . " WHERE order_id=" . intval($order_id);
	$all_goods_number  = 0;
	$all_order_mount   = 0.00;
	$res = $GLOBALS['db']->query($sql);
	while ($row = $GLOBALS['db']->fetchRow($res)){
		 $all_goods_number += $row['goods_number'];
		 $actual_price = floatval($row['norm_price'] * $row['discount']);
		 $actual_mount = floatval($actual_price * $row['goods_number']);
		 $all_order_mount += $actual_mount;		
	}
	$arr_amount['goods_number'] = intval($all_goods_number);
	$arr_amount['order_amount'] = floatval($all_order_mount);		
    return $arr_amount;    
}   

function jxc_order_action($type, $order_id){	
	switch ($type){
		default:return false; break;
		case 'pcs':
			$ctype = 0;
			break;			
		case 'enter':
			$ctype = 1;
			break;
		case 'return':
			$ctype = 2;
			break;			
		case 'check':
			$ctype = 3;
			break;	
		case 'loss':	
			$ctype = 4;
			break;	
		case 'rectify':	
			$ctype = 5;
			break;	
		case 'balance':
			$ctype = 6;
			break;  
	}
	$act_list = array();
	$sql = "SELECT * FROM " . $GLOBALS['os']->table('purchase_order_action') . " WHERE ctype='$ctype' AND order_id='$order_id' ORDER BY log_time DESC";
	$res = $GLOBALS['db']->query($sql);
	while ($row = $GLOBALS['db']->fetchRow($res))
	{
		$row['order_status']    = jsc_order_status($type, $row['order_status']);
		$row['action_time']     = local_date($GLOBALS['_CFG']['time_format'], $row['log_time']);
		$act_list[] = $row;
	}	
	return $act_list;
}

function update_jxc_order($type, $order_id, $order)
{ 
	switch ($type){
		default:return false;break;
		case 'pcs':
			$tb='purchase_order';
			break;
		case 'enter':
			$tb = 'purchase_confirm_order';
			break;
		case 'check':
			$tb = 'purchase_check_order';
			break;
		case 'loss':
			$tb = 'purchase_loss_order';
			break;
		case 'rectify':
			$tb = 'purchase_rectify_order';			
			break;
		case 'return':
			$tb = 'purchase_return_order';
			break;
		case 'balance':
			$tb = 'purchase_balance_order';
	}
    return $GLOBALS['db']->autoExecute($GLOBALS['os']->table($tb),
           $order, 'UPDATE', "order_id = '$order_id'");
}

function jsc_order_status($type, $status=NULL){
	$arr_type = array('pcs', 'enter', 'return', 'check', 'loss', 'rectify', 'balance');
	$arr_status =array();
	$arr_status['pcs']     = array(0=>'待处理', 1=>'仓库审核通过', 2=>'仓库审核不通过', 3=>'财务审核通过', 4=>'财务审核不通过', 5=>'已生成入库单', 6=>'完成', 7=>'作废');
	$arr_status['enter']   = array(0=>'待处理', 1=>'已验收', 2=>'已入库', 3=>'已结算', 4=>'已作废');
	$arr_status['return']  = array(0=>'待处理', 1=>'审核通过', 2=>'审核不通过', 3=>'结算', 4=>'作废');
	$arr_status['check']   = array(0=>'待处理', 1=>'确认', 2=>'作废', 3=>'已盈亏');
	$arr_status['loss']    = array(0=>'待处理', 1=>'确认', 2=>'作废');
	$arr_status['rectify'] = array(0=>'待处理', 1=>'确认', 2=>'调整', 3=>'回滚', 4=>'作废', 5=>'完成');
	$arr_status['balance'] = array(0=>'待处理', 1=>'结算', 2=>'作废');
		
	if(!in_array($type, $arr_type)) return false; 	
    $list_status = $arr_status[$type];
	if(is_numeric($status) && isset($list_status[$status])){
		$list_status = $list_status[$status];
	}
	return $list_status;
}

function get_range_color_list(){
    $result = get_filter();
    if ($result === false)
    {
        /* 初始化分页参数 */
        $filter = array();
        $filter['sort_by']     = empty($_REQUEST['sort_by']) ? 'c.color_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order']  = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
        $filter['range_type']  = empty($_REQUEST['range_type']) ? '' : trim($_REQUEST['range_type']);
        $filter['keyword']     = empty($_REQUEST['keyword']) ? '' : trim($_REQUEST['keyword']);
        if ($_REQUEST['is_ajax'] == 1)
        {
            $filter['keyword'] = json_str_iconv($filter['keyword']);
        }		
        $ext_sql = '';
        if(!empty($filter['keyword'])){
        	switch ($filter['range_type']){
        		default:break;
        		case 'name':
        			$ext_sql = " AND c.color_name LIKE '%$filter[keyword]%' ";
        			break;
        		case 'code':
        			$ext_sql = " AND c.color_code LIKE '%$filter[keyword]%' ";
        			break;
        	}
        }
        if(!empty($ext_sql)){
        	$ext_sql = ' WHERE 1 ' . $ext_sql;
        }
        /* 查询记录总数，计算分页数 */
   		$sql = "SELECT COUNT(*) FROM " . $GLOBALS['os']->table('color') . " c LEFT JOIN " . 
   			   $GLOBALS['os']->table('color_lib') . " cl ON c.img_id=cl.img_id " . $ext_sql;
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);
        $filter = page_and_size($filter);

        /* 查询记录 */
        $sql = "SELECT c.* FROM " . $GLOBALS['os']->table('color') . " c LEFT JOIN " . $GLOBALS['os']->table('color_lib') . 
        	   " cl ON c.img_id=cl.img_id $ext_sql ORDER BY $filter[sort_by] $filter[sort_order]";
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
//    	$row['supply_type'] = $GLOBALS['_LANG']['supply_types'][$row['supply_type']];
        $arr[] = $row;
    }
    return array('color' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);	
}

function get_range_size_list(){
    $result = get_filter();
    if ($result === false)
    {
        /* 初始化分页参数 */
        $filter = array();
        $filter['sort_by']     = empty($_REQUEST['sort_by']) ? 's.size_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order']  = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
        $filter['range_type']  = empty($_REQUEST['range_type']) ? '' : trim($_REQUEST['range_type']);
        $filter['keyword']     = empty($_REQUEST['keyword']) ? '' : trim($_REQUEST['keyword']);
        if ($_REQUEST['is_ajax'] == 1)
        {
            $filter['keyword'] = json_str_iconv($filter['keyword']);
        }		
        $ext_sql = '';
        if(!empty($filter['keyword'])){
        	switch ($filter['range_type']){
        		default:break;
        		case 'name':
        			$ext_sql = " AND s.size_name LIKE '%$filter[keyword]%' ";
        			break;
        		case 'code':
        			$ext_sql = " AND s.size_code LIKE '%$filter[keyword]%' ";
        			break;
        	}
        }
        if(!empty($ext_sql)){
        	$ext_sql = ' WHERE 1 ' . $ext_sql;
        }
        /* 查询记录总数，计算分页数 */
   		$sql = "SELECT COUNT(*) FROM " . $GLOBALS['os']->table('size') . " s " . $ext_sql;
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);
        $filter = page_and_size($filter);

        /* 查询记录 */
        $sql = "SELECT s.* FROM " . $GLOBALS['os']->table('size') . " s $ext_sql ORDER BY $filter[sort_by] $filter[sort_order]";
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $arr[] = $row;
    }
    return array('size' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);		
}

function get_range_goods_list(){	
    $result = get_filter();
    if ($result === false)
    {
        /* 初始化分页参数 */
        $filter = array();
        $filter['sort_by']     = empty($_REQUEST['sort_by']) ? 'goods_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order']  = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
        $filter['cat_id2']  = empty($_REQUEST['cat_id2']) ? 0 : intval($_REQUEST['cat_id2']);
        $filter['brand_id2']  = empty($_REQUEST['cat_id2']) ? 0 : intval($_REQUEST['brand_id2']);        
        $filter['keyword2']     = empty($_REQUEST['keyword2']) ? '' : trim($_REQUEST['keyword2']);
        if ($_REQUEST['is_ajax'] == 1)
        {
            $filter['keyword2'] = json_str_iconv($filter['keyword2']);
        }		
        $ext_sql = '';
        if(!empty($filter['keyword2'])){
        	if($filter['cat_id2'] > 0){
        		
        	}
        	if($filter['brand_id2'] > 0){
        		
        	}
        	switch ($filter['range_type']){
        		default:break;
        		case 'name':
        			$ext_sql = " AND s.supplier_name LIKE '%$filter[keyword]%' ";
        			break;
        		case 'code':
        			$ext_sql = " AND s.supplier_code LIKE '%$filter[keyword]%' ";
        			break;
        	}
        }
        /* 查询记录总数，计算分页数 */
   		$sql = "SELECT COUNT(*) FROM " . $GLOBALS['os']->table('goods') . " g WHERE g.is_delete=0 AND g.is_real=1 " . $ext_sql;
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);
        $filter = page_and_size($filter);

        /* 查询记录 */
        $sql = "SELECT g.* FROM " . $GLOBALS['os']->table('goods') . " g WHERE g.is_delete=0 AND g.is_real=1 $ext_sql ORDER BY $filter[sort_by] $filter[sort_order]";
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $arr[] = $row;
    }
    return array('goods' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}


function get_pcs_order_goods_list(){
    $result = get_filter();
    if ($result === false)
    {
        /* 初始化分页参数 */
        $filter = array();
        $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'rec_id' : trim($_REQUEST['sort_by']);
        $filter['sort_by']    = $filter['sort_by']=='goods_id' ? 'pog.goods_id' : $filter['sort_by'];
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
		$filter['pcs_order_id'] = empty($_REQUEST['pcs_order_id']) ? 0 : intval($_REQUEST['pcs_order_id']);
		
        /* 查询记录总数，计算分页数 */
        $sql = "SELECT count(*) FROM " . $GLOBALS['os']->table('purchase_order_goods') . " pog LEFT JOIN " . 
   		       $GLOBALS['os']->table('goods') . " g ON pog.goods_id=g.goods_id LEFT JOIN " . 
   		       $GLOBALS['os']->table('color') . " c ON pog.color_id=c.color_id LEFT JOIN " .
   		       $GLOBALS['os']->table('size') . " s ON pog.size_id=s.size_id " .
   		       " WHERE pog.purchase_order_id=" . $filter['pcs_order_id'];
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);
        $filter = page_and_size($filter);

        /* 查询记录 */
        $sql = "SELECT pog.*,g.goods_name,c.color_name,s.size_name FROM " . $GLOBALS['os']->table('purchase_order_goods') . " pog LEFT JOIN " . 
   		       $GLOBALS['os']->table('goods') . " g ON pog.goods_id=g.goods_id LEFT JOIN " . 
   		       $GLOBALS['os']->table('color') . " c ON pog.color_id=c.color_id LEFT JOIN " .
   		       $GLOBALS['os']->table('size') . " s ON pog.size_id=s.size_id " .
   		       " WHERE pog.purchase_order_id=" . $filter['pcs_order_id'] . 
   		       " ORDER BY $filter[sort_by] $filter[sort_order]";
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $arr[] = $row;
    }
    return array('order_goods' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);		
}


function status_alter_to_balance($order_type, $order_id){
	if($order_type==1){
		$all_amount = $GLOBALS['db']->getOne("SELECT order_amount FROM " . $GLOBALS['os']->table('purchase_confirm_order') . " WHERE order_id='$order_id'");    	
	}elseif($order_type==2){
		$all_amount = $GLOBALS['db']->getOne("SELECT order_amount FROM " . $GLOBALS['os']->table('purchase_return_order') . " WHERE order_id='$order_id'");  
	}   
    $payed_amount = $GLOBALS['db']->getOne("SELECT SUM(pay_amount) FROM " . $GLOBALS['os']->table('purchase_balance_order_detail') . 
    							" WHERE order_type='$order_type' AND from_order_id='$order_id'");
    if($payed_amount >= $all_amount){
		if($order_type==1){	
			$GLOBALS['db']->query("UPDATE " . $GLOBALS['os']->table('purchase_confirm_order') . " SET order_status=3 WHERE order_id='$order_id'");
		}elseif($order_type==2){
			$GLOBALS['db']->query("UPDATE " . $GLOBALS['os']->table('purchase_return_order') . " SET order_status=3 WHERE order_id='$order_id'");
		}		
    }	
}





function esd_order_action($ctype, $order_sn, $order_status, $note = '', $username = null){
	switch ($ctype){
		default: return; break;
		case 'pcs':
			$tb = 'purchase_order';
			$ctype = 0;
			break;
		case 'confirm_order':
			$tb = 'purchase_confirm_order';	
			$ctype = 1;	
			break;
		case 'return':
			$tb = 'purchase_return_order';	
			$ctype = 2;	
			break;			
		case 'check':
			$tb = 'purchase_check_order';	
			$ctype = 3;				
			break;
		case 'loss':
			$tb = 'purchase_loss_order';	
			$ctype = 4;	
			break;
		case 'rectify':
			$tb = 'purchase_rectify_order';	
			$ctype = 5;	
			break;
		case 'balance':
			$tb = 'purchase_balance_order';	
			$ctype = 6;	
			break;		
	}	
    if (is_null($username))
    {
        $username = $_SESSION['admin_name'];
    }

    $sql = 'INSERT INTO ' . $GLOBALS['os']->table('purchase_order_action') .
           ' (ctype, order_id, action_user, order_status, action_note, log_time) ' .
           "SELECT '$ctype', order_id, '$username', '$order_status', '$note', '" .
            gmtime() . "' FROM " . $GLOBALS['os']->table($tb) . " WHERE order_sn = '$order_sn'";            

    $GLOBALS['db']->query($sql);
}

function pcs_order_goods_list($pcs_order_id = 0)
{
		$sql = "SELECT pog.goods_id, g.goods_name, g.goods_sn FROM " . $GLOBALS['os']->table('purchase_order_goods') . 
		       ' pog LEFT JOIN ' . $GLOBALS['os']->table('goods') . ' g ON pog.goods_id=g.goods_id ' . 
		       ' WHERE pog.purchase_order_id=' . $pcs_order_id . ' GROUP BY pog.goods_id ORDER BY pog.rec_id DESC';
		$result = $GLOBALS['db']->getAll($sql);
	    
}



function create_order_sn($type, $prefix='')
{
	$arr_type = array('pcs', 'confirm', 'check', 'loss', 'rectify', 'return', 'balance');
	if(!in_array($type, $arr_type)) return false;
	switch ($type) 
	{
		default:                return false;                           break;
		case 'pcs':			    $tb = 'purchase_order';			        break;
		case 'confirm':			$tb = 'purchase_confirm_order';			break;
		case 'check':			$tb = 'purchase_check_order';			break;
		case 'loss':            $tb = 'purchase_loss_order'; 		    break;
		case 'rectify':         $tb = 'purchase_rectify_order';         break;
		case 'return':          $tb = 'purchase_return_order';          break;
		case 'balance':         $tb = 'purchase_balance_order';         break;
	}
    $sn = '';
    while (empty($sn))
    {
        $sn = empty($prefix) ? mt_rand() : ($prefix . mt_rand());
        if ($GLOBALS['db']->getOne("SELECT order_sn FROM ".$GLOBALS['os']->table($tb)." WHERE order_sn='$sn'"))
        {
            $sn = '';
        }
    }
    return $sn;	
}

function get_pays_list(){
	static $pays_list = array();
	$result = $GLOBALS['db']->query("SELECT * FROM ".$GLOBALS['os']->table('purchase_payment')." WHERE is_show=1");
	while ($row = $GLOBALS['db']->fetchRow($result)) {
		$pays_list[$row['pay_id']] = $row['pay_name'];
	}
	return $pays_list;
}
    
function create_pcs_rectify_order($order_id, $order_sn)
{	    
    $sql = "INSERT INTO " . $GLOBALS['os']->table('purchase_rectify_order') . "(order_sn, depot_id, " . 
    	   "seat_id, discount, goods_number, goods_number_rectify, order_amount, order_status, order_brief," . 
    	   " add_time) SELECT '$order_sn', depot_id, seat_id, discount, goods_number, goods_number_loss," . 
    	   " order_amount, 0, '', " . gmtime() . " FROM " . $GLOBALS['os']->table('purchase_loss_order') . 
    	   " WHERE order_id=" . intval($order_id);
    $res = $GLOBALS['db']->query($sql);	
    if ($res !== false)
    {    
    	$insert_id = $GLOBALS['db']->insert_id();
    	$sql = "UPDATE " . $GLOBALS['os']->table('purchase_loss_order') . 
    		   " SET rectify_order_id='$insert_id' WHERE order_id=" . intval($order_id);
    	$GLOBALS['db']->query($sql);
    	return $insert_id;
    }
    else
    {
        return false;
    } 
}

function create_pcs_rectify_order_goods($order_id, $loss_order_id)
{
    $sql = "INSERT INTO " . $GLOBALS['os']->table('purchase_rectify_order_goods') . "(order_id, goods_id, color_id, " . 
    	   "size_id,goods_number,goods_number_inventory,goods_number_rectify,norm_price,discount,actual_price," . 
    	   "norm_amount,actual_mount) SELECT " . intval($order_id) . ",goods_id,color_id,size_id,goods_number," . 
    	   "goods_number_inventory,goods_number_loss,norm_price,discount,actual_price,norm_amount,actual_mount FROM " . 
    	   $GLOBALS['os']->table('purchase_loss_order_goods')." WHERE order_id=".intval($loss_order_id);
    $res = $GLOBALS['db']->query($sql);
    
    if ($res !== false)
    {    
    	return true;
    }
    else
    {
        return false;
    } 
}

function create_pcs_loss_order($order_id, $order_sn)
{	
    $sql = "INSERT INTO " . $GLOBALS['os']->table('purchase_loss_order') . "(order_sn, depot_id, seat_id, " . 
    	   "discount, goods_number, order_amount, order_status, order_brief, add_time) SELECT '$order_sn', " . 
    	   "depot_id, seat_id, discount, goods_number, order_amount, 0, '', " . gmtime() . " FROM " . 
    	   $GLOBALS['os']->table('purchase_check_order') . " WHERE order_id=" . intval($order_id);
    $res = $GLOBALS['db']->query($sql);	
    if ($res !== false)
    {    
    	return $GLOBALS['db']->insert_id();
    }
    else
    {
        return false;
    } 
}

function create_pcs_loss_order_goods($order_id, $check_order_id)
{	
	$sql = "SELECT goods_id,color_id,size_id,goods_number,norm_price,discount,actual_price,norm_amount,actual_mount FROM " . 
		   $GLOBALS['os']->table('purchase_check_order_goods') . " WHERE order_id=" . intval($check_order_id);
	$res = $GLOBALS['db']->query($sql);
	while ($row=$GLOBALS['db']->fetchRow($res)) {
		$sql_in = "SELECT goods_number_actual FROM " . $GLOBALS['os']->table('goods_inventory') . 
		          " WHERE goods_id=$row[goods_id] AND color_id=$row[color_id] AND size_id=$row[size_id]";
		$inventory_num = $GLOBALS['db']->getOne($sql_in);	
		$inventory_num = is_numeric($inventory_num) ? intval($inventory_num) : 0;
    	$sql_ls = "INSERT INTO " . $GLOBALS['os']->table('purchase_loss_order_goods') . "(order_id, goods_id, color_id, " . 
    	          "size_id, goods_number, goods_number_inventory, goods_number_loss, norm_price, discount, actual_price, " . 
    	          "norm_amount, actual_mount) VALUES (" . intval($order_id) . ", $row[goods_id], $row[color_id], " . 
    	          "$row[size_id], $row[goods_number], $inventory_num, " . intval($inventory_num-$row['goods_number']) . 
    	          ",$row[norm_price], $row[discount], $row[actual_price], $row[norm_amount], $row[actual_mount])";
    	$GLOBALS['db']->query($sql_ls);			
	}


    if ($res !== false)
    {    
    	return true;
    }
    else
    {
        return false;
    } 
}

function show_order_goods_list($tb, $order_id, $goods_id){
	$sql =  "SELECT og.*,g.goods_name,g.goods_sn,IFNULL(c.color_name,'默认') color_name,
	        IFNULL(s.size_name,'默认') size_name,IFNULL(b.brand_name, '') AS brand_name FROM " . 
	        $GLOBALS['os']->table($tb) . ' og LEFT JOIN ' . $GLOBALS['os']->table('goods') . 
	        ' g ON og.goods_id=g.goods_id LEFT JOIN ' . $GLOBALS['os']->table('color') . 
	        ' c ON og.color_id=c.color_id LEFT JOIN ' . $GLOBALS['os']->table('size') . 
	        ' s ON og.size_id=s.size_id LEFT JOIN ' . $GLOBALS['os']->table('brand') . 
	        ' b ON g.brand_id=b.brand_id WHERE og.order_id=' . intval($order_id) . 
	        ' AND og.goods_id=' . intval($goods_id) . ' ORDER BY og.rec_id DESC';
	$result = $GLOBALS['db']->query($sql);
	$goods_list = array();
	while ($row = $GLOBALS['db']->fetchRow($result)) {
		$goods_list[$row['rec_id']]['goods_name']              =  substr($row['goods_name'], 0);
		$goods_list[$row['rec_id']]['brand_name']              =  substr($row['brand_name'], 0);
		$goods_list[$row['rec_id']]['goods_sn']                =  substr($row['goods_sn'], 0);
		$goods_list[$row['rec_id']]['formated_norm_price']     =  price_format($row['norm_price']);		
		$goods_list[$row['rec_id']]['formated_discount']       =  sprintf('%s', floatval($row['discount']));
		$goods_list[$row['rec_id']]['formated_actual_price']   =  price_format($row['actual_price']);
		$goods_list[$row['rec_id']]['goods_number']            =  $row['goods_number'];
		$goods_list[$row['rec_id']]['goods_number_inventory']  =  $row['goods_number_inventory'];
		$goods_list[$row['rec_id']]['formated_num_loss']  	   =  ($row['goods_number_loss'] > 0 ? '-' : '+') . abs($row['goods_number_loss']);
		$goods_list[$row['rec_id']]['formated_num_rectify']    =  ($row['goods_number_rectify'] > 0 ? '-' : '+') . abs($row['goods_number_rectify']);
		$goods_list[$row['rec_id']]['goods_attr']              =  '颜色:' . $row['color_name'] . '&nbsp;尺码:' . $row['size_name'];
		$goods_list[$row['rec_id']]['formated_subtotal']       =  price_format($row['actual_price'] * $row['goods_number']);
	    $goods_list[$row['rec_id']]['formated_loss_subtotal']  =  price_format($row['actual_price'] * (($row['goods_number_loss'] > 0 ? '-' : '+') . abs($row['goods_number_loss'])));
	}  
	return $goods_list;	
}

function pcs_balance_order_list(){
    $result = get_filter();
    if ($result === false)
    {
        /* 初始化分页参数 */
        $filter = array();
        $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'order_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
        $filter['supplier_id']   = empty($_REQUEST['supplier_id']) ? 0 : intval($_REQUEST['supplier_id']);
        $filter['start_add_time']      = empty($_REQUEST['start_add_time']) ? '' : (strpos($_REQUEST['start_add_time'], '-') > 0 ?  local_strtotime($_REQUEST['start_add_time']) : $_REQUEST['start_add_time']);
        $filter['end_add_time']        = empty($_REQUEST['end_add_time']) ? '' : (strpos($_REQUEST['end_add_time'], '-') > 0 ?  local_strtotime($_REQUEST['end_add_time']) : $_REQUEST['end_add_time']);
        
        $where = 'WHERE 1 ';
        if ($filter['supplier_id'])
        {
            $where .= " AND o.supplier_id='$filter[supplier_id]'";
        }
        if ($filter['start_add_time'])
        {
            $where .= " AND o.add_time >= '$filter[start_add_time]'";
        }
        if ($filter['end_add_time'])
        {
            $where .= " AND o.add_time <= '$filter[end_add_time]'";
        }
                   
        /* 查询记录总数，计算分页数 */
   		$sql = "SELECT COUNT(*) FROM " . $GLOBALS['os']->table('purchase_confirm_order') . " o LEFT JOIN " . 
   			   $GLOBALS['os']->table('supplier') . " s ON o.supplier_id=s.supplier_id " . $where;
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);
   		$sql = "SELECT COUNT(*) FROM " . $GLOBALS['os']->table('purchase_return_order') . " o LEFT JOIN " . 
   			   $GLOBALS['os']->table('supplier') . " s ON o.supplier_id=s.supplier_id " . $where;
        $filter['record_count'] += $GLOBALS['db']->getOne($sql);
        $filter = page_and_size($filter);

        /* 查询记录 */
        $sql = "SELECT o.order_id,o.order_sn,o.goods_number,o.order_amount,1 order_type,o.add_time,s.supplier_name FROM " . 
               $GLOBALS['os']->table('purchase_confirm_order') . " o LEFT JOIN " . $GLOBALS['os']->table('supplier') . 
               " s ON o.supplier_id=s.supplier_id $where " . " UNION SELECT o.order_id,o.order_sn,o.goods_number,o.order_amount," . 
               "2 order_type,o.add_time,s.supplier_name FROM " . $GLOBALS['os']->table('purchase_return_order') . " o LEFT JOIN " . 
               $GLOBALS['os']->table('supplier') . " s ON o.supplier_id=s.supplier_id $where ORDER BY $filter[sort_by] $filter[sort_order]";
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->query($sql);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
    	$payed_amount = $GLOBALS['db']->getOne("SELECT SUM(pay_amount) FROM " . $GLOBALS['os']->table('purchase_balance_order_detail') . 
    										" WHERE order_type=" . intval($row['order_type']) . " AND from_order_id=" . intval($row['order_id']));
        if($payed_amount < $row['order_amount']){
        	$row['supplier_name']	            = sub_str($row['supplier_name'], 12);
	      	$row['formated_order_amount']	    = price_format($row['order_amount']);
	      	$row['formated_payed_amount']	    = price_format($payed_amount);
	      	$row['formated_add_time']           = local_date('Y-m-d', $row['add_time']);
	        $arr[] = $row;
        }
    }
    return array('item' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);	
}

function get_balance_amount($arr_order){
	if(!is_array($arr_order)) return '';
	$pcs_amount = $GLOBALS['db']->getOne('SELECT SUM(order_amount) FROM ' . $GLOBALS['os']->table('purchase_confirm_order') . ' WHERE ' . db_create_in($arr_order[0], 'order_id'));
	$return_amount = $GLOBALS['db']->getOne('SELECT SUM(order_amount) FROM ' . $GLOBALS['os']->table('purchase_return_order') . ' WHERE ' . db_create_in($arr_order[1], 'order_id'));
	$balance_amount = floatval($pcs_amount-$return_amount);
	return $balance_amount;
}

function balance_amount($order_id){
	$sql = "SELECT order_type,pay_amount FROM " . $GLOBALS['os']->table('purchase_balance_order_detail') . " WHERE order_id=" . intval($order_id);
	$res = $GLOBALS['db']->query($sql);
	$balance_amount = 0.00;
    while ($row = $GLOBALS['db']->fetchRow($res)) {
    	if($row['order_type']==2){
    		$balance_amount -= $row['pay_amount'];
    	}elseif($row['order_type']==1){
    		$balance_amount += $row['pay_amount'];
    	}
    }
    $balance_amount = floatval($balance_amount);
	return $balance_amount;
}

function get_pcs_balance_order_list(){
    $result = get_filter();
    if ($result === false)
    {
        /* 初始化分页参数 */
        $filter = array();
        $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'order_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
        $filter['order_sn'] 		= empty($_REQUEST['order_sn']) ? '' : trim($_REQUEST['order_sn']);
        if (!empty($_GET['is_ajax']) && $_GET['is_ajax'] == 1)
        {
            $_REQUEST['supplier_name'] = json_str_iconv($_REQUEST['supplier_name']);
        }
        $filter['supplier_name']   = empty($_REQUEST['supplier_name']) ? '' : trim($_REQUEST['supplier_name']);
        $filter['status']     		= (!is_numeric($_REQUEST['status']) || $_REQUEST['status']<0) ? -1 : intval($_REQUEST['status']);
        $filter['balance_type']     = (!is_numeric($_REQUEST['balance_type']) || $_REQUEST['balance_type']<0) ? -1 : intval($_REQUEST['balance_type']);
        $filter['pay_type']     	= (!is_numeric($_REQUEST['pay_type']) || $_REQUEST['pay_type']<0) ? -1 : intval($_REQUEST['pay_type']);
        $filter['start_add_time']      = empty($_REQUEST['start_add_time']) ? '' : (strpos($_REQUEST['start_add_time'], '-') > 0 ?  local_strtotime($_REQUEST['start_add_time']) : $_REQUEST['start_add_time']);
        $filter['end_add_time']        = empty($_REQUEST['end_add_time']) ? '' : (strpos($_REQUEST['end_add_time'], '-') > 0 ?  local_strtotime($_REQUEST['end_add_time']) : $_REQUEST['end_add_time']);
        $filter['start_deliver_time']  = empty($_REQUEST['start_deliver_time']) ? '' : (strpos($_REQUEST['start_deliver_time'], '-') > 0 ?  local_strtotime($_REQUEST['start_deliver_time']) : $_REQUEST['start_deliver_time']);
        $filter['end_deliver_time']    = empty($_REQUEST['end_deliver_time']) ? '' : (strpos($_REQUEST['end_deliver_time'], '-') > 0 ?  local_strtotime($_REQUEST['end_deliver_time']) : $_REQUEST['end_deliver_time']);
                
        $where = 'WHERE 1 ';
        if ($filter['supplier_name'])
        {
            $where .= " AND s.supplier_name LIKE '%" . mysql_like_quote($filter['supplier_name']) . "%'";
        }  
        if($filter['status']>=0){
        	$where .= " AND o.order_status='$filter[status]'";
        }  
        if($filter['balance_type']>=0){
        	$where .= " AND o.balance_type='$filter[balance_type]'";
        }  
        if($filter['pay_type']>=0){
        	$where .= " AND o.pay_type='$filter[pay_type]'";
        }  
        if ($filter['start_add_time'])
        {
            $where .= " AND o.add_time >= '$filter[start_add_time]'";
        }
        if ($filter['end_add_time'])
        {
            $where .= " AND o.add_time <= '$filter[end_add_time]'";
        }
        if ($filter['start_deliver_time'])
        {
            $where .= " AND o.deliver_time >= '$filter[start_deliver_time]'";
        }
        if ($filter['end_deliver_time'])
        {
            $where .= " AND o.deliver_time <= '$filter[end_deliver_time]'";
        }  
                   
        /* 查询记录总数，计算分页数 */
   		$sql = "SELECT COUNT(*) FROM " . $GLOBALS['os']->table('purchase_balance_order') . " o LEFT JOIN " . 
   		       $GLOBALS['os']->table('supplier') . " s ON o.supplier_id=s.supplier_id LEFT JOIN " . 
   		       $GLOBALS['os']->table('purchase_payment') . " p ON o.pay_type=p.pay_id " . $where;

        $filter['record_count'] = $GLOBALS['db']->getOne($sql);
        $filter = page_and_size($filter);

        /* 查询记录 */
        $sql = "SELECT o.*,s.supplier_name,p.pay_name FROM " . $GLOBALS['os']->table('purchase_balance_order') . 
               " o LEFT JOIN " . $GLOBALS['os']->table('supplier') . " s ON o.supplier_id=s.supplier_id LEFT JOIN " . 
   		       $GLOBALS['os']->table('purchase_payment') . " p ON o.pay_type=p.pay_id " .  
               $where . " ORDER BY $filter[sort_by] $filter[sort_order]";
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
    	$row['supplier_name']               = sub_str($row['supplier_name'], 10);
      	$row['status'] 					    = jsc_order_status('balance', $row['order_status']);
      	$row['formated_order_amount']	    = price_format($row['order_amount']);
      	$row['formated_pay_amount']	        = price_format($row['pay_amount']);
      	$row['formated_surplus_amount']	    = price_format($row['surplus_amount']);
      	$row['formated_deliver_time']       = local_date('Y-m-d', $row['deliver_time']);
      	$row['formated_add_time']           = local_date('Y-m-d', $row['add_time']);
      	$row['pay_type_name']               = sub_str($row['pay_name'], 8);
        $arr[] = $row;
    }
    return array('item' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);		
}

function pcs_payment_list(){
   $result = get_filter();
    if ($result === false)
    {
        /* 初始化分页参数 */
        $filter = array();
        $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'pay_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
      	if (!empty($_GET['is_ajax']) && $_GET['is_ajax'] == 1)
        {
            $_REQUEST['pay_name'] = json_str_iconv($_REQUEST['pay_name']);
        }
        $filter['pay_name']   = empty($_REQUEST['pay_name']) ? '' : trim($_REQUEST['pay_name']);
        $filter['pay_code']   = empty($_REQUEST['pay_code']) ? '' : trim($_REQUEST['pay_code']);
        $where = 'WHERE 1 ';
        if ($filter['pay_name'])
        {
            $where .= " AND pay_name LIKE '%" . mysql_like_quote($filter['pay_name']) . "%'";
        }

        if ($filter['pay_code'])
        {
            $where .= " AND pay_code LIKE '%" . mysql_like_quote($filter['pay_code']) . "%'";
        }
        /* 查询记录总数，计算分页数 */
   		$sql = "SELECT COUNT(*) FROM " . $GLOBALS['os']->table('purchase_payment') . $where;

        $filter['record_count'] = $GLOBALS['db']->getOne($sql);
        $filter = page_and_size($filter);

        /* 查询记录 */
        $sql = "SELECT * FROM " . $GLOBALS['os']->table('purchase_payment') . $where . " ORDER BY $filter[sort_by] $filter[sort_order]";
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $arr[] = $row;
    }
    return array('item' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}
?>